package zooi;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class SocketDingen {

  public static PrintWriter schrijf;
  public static BufferedReader lees;
  public static String adres = "localhost";
  public static int poort = 8765;
  public static Socket client;
  public static String tekst,  verzenden;

  public SocketDingen() {
    verbindserver();
    if (client.isConnected()) {
      maakvariabelen();
      communiceerserver();
    }
  }
  public static JTextArea txtAr;
  public static JPanel pan;
  public static JTextField veld;
  public static JButton knop;

  public static void SocketDingen(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLayout(new BorderLayout());
    txtAr = new JTextArea();
    txtAr.setText("Lucky Bank Server");
    txtAr.setEditable(false);
    txtAr.setFont(new Font("Monospaced", Font.PLAIN, 12));
    JScrollPane sp = new JScrollPane(txtAr);
    frame.add(sp, BorderLayout.CENTER);
    pan = new JPanel();
    pan.setLayout(new GridLayout());
    veld = new JTextField();
    knop = new JButton("Zend");
    knop.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        zend(veld.getText());
      }
    });
    pan.add(veld);
    pan.add(knop);
    frame.add(pan,BorderLayout.SOUTH);
    loggen("LOL");
    frame.setVisible(true);
    new SocketDingen();
  }

  void communiceerserver() {
    new Thread(new Ontvang()).start();
  }

  public static void zend(String tekst) {
    if (tekst != null) {
      schrijf.println(tekst);
    loggen("CLIENT: verzenden = " + tekst);
    } else {
      schrijf.println("XXX");
      loggen("CLIENT: gestopt");
      System.exit(0);
    }
  }

  void verbindserver() {
    try {
      client = new Socket("127.0.0.1", poort);
      //   verbonden = true;
      loggen("CLIENT: verbonden met server");
    } catch (Exception e) {
      loggen("CLIENT: server niet beschikbaar" + e);
      message("de server is niet beschikbaar");
      System.exit(0);
    }
  }

  void maakvariabelen() {
    try {
      lees = new BufferedReader(new InputStreamReader(client.getInputStream()));
      schrijf = new PrintWriter(client.getOutputStream(), true);
    } catch (Exception e) {
    }
  }

  public static void loggen(String tekst) {
    drukaf(tekst);
    System.out.println(tekst);
  }

  public static void drukaf(String t) {
    txtAr.setText(txtAr.getText() + "\n" + t);
  }

  void message(String tekst) {
    JOptionPane.showMessageDialog(null, tekst, "CLIENT", 2);
  }

  class Ontvang implements Runnable {

    String readLine = "";

    public void run() {
      try {
        while ((readLine = lees.readLine()) != null) {
          loggen(readLine);
          //System.err.println(readLine);
        }
      } catch (Exception e) {
      }
    }
  }
}